package weiyao.xinxidasai.Utils.Encryption.ARGON2;



import de.mkammerer.argon2.Argon2Advanced;
import de.mkammerer.argon2.Argon2Factory;

/**
 * 基于argon2-jvm包调用
 */
public class Argon2PasswordEncoder  {

    /*
     * 设置自定义参数
     */
    private static final byte[] FIXED_SALT = "YourFixedSaltValue".getBytes();
    private static final int HASH_LENGTH = 16; // 指定哈希长度为32字节
    private static final int ITERATIONS_normal = 3;
    private static final int MEMORY_normal = 65536;
    private static final int PARALLELISM_normal = 2;
    private static final int ITERATIONS_high = 5;
    private static final int MEMORY_high = 131072;
    private static final int PARALLELISM_high = 4;
    private static final int ITERATIONS_EX = 7;
    private static final int MEMORY_EX = 262144;
    private static final int PARALLELISM_EX = 8;

    /*
     * 推荐 argon2id类型共有三种类型可选（ARGON2i,ARGON2d,ARGON2id;）
     */
    public static final Argon2Factory.Argon2Types TYPE = Argon2Factory.Argon2Types.ARGON2id;

    private static final Argon2Advanced INSTANCE = Argon2Factory.createAdvanced(
            TYPE,
            FIXED_SALT.length, // 盐值长度
            HASH_LENGTH        // 哈希长度
    );


    /**
     * 标准安全级别的加密方法
     * 适用于常规安全要求的场景
     */
    public static String encode_normal(String password) {
        return INSTANCE.hash(
                ITERATIONS_normal,  // 3次迭代
                MEMORY_normal,      // 64MB内存
                PARALLELISM_normal, // 2个并行线程
                password.toCharArray()
        );
    }

    /**
     * 高安全级别的加密方法
     * 适用于对安全性要求较高的场景
     */
    public static String encode_high(String password) {
        return INSTANCE.hash(
                ITERATIONS_high,    // 5次迭代
                MEMORY_high,        // 128MB内存
                PARALLELISM_high,   // 4个并行线程
                password.toCharArray()
        );
    }

    /**
     * 极高安全级别的加密方法
     * 适用于对安全性要求极高的场景
     */
    public static String encode_EX(String password) {
        return INSTANCE.hash(
                ITERATIONS_EX,      // 7次迭代
                MEMORY_EX,          // 256MB内存
                PARALLELISM_EX,     // 8个并行线程
                password.toCharArray()
        );
    }

    /**
     * 使用argon2id算法验证密码是否正确
     * @param encodedPassword 加密后密码
     */
    public static boolean matches(String encodedPassword, String password) {
        return INSTANCE.verify(encodedPassword, password.toCharArray());
    }



}

